package com.zhuhjay.common.security;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.time.LocalDateTime;
import java.util.Collection;

/**
 * 对用户的信息进行装饰
 * @author ZhuHJay
 * @date 2023/2/25 10:19
 */
@Data
public class LoginUserDetail implements UserDetails {

    private int id;
    private String username;
    private long uid;
    /** 登录时间 **/
    private LocalDateTime loginTime;
    /** 过期时间 **/
    private LocalDateTime expireTime;
    /** 用户信息 **/
    private UserInfo user;

    /** 用户登录ip **/
    private String ip;
    /** 用户登录地点 **/
    private String location;
    /** 浏览器类型 **/
    private String browser;
    /** 操作系统 **/
    private String os;

    public LoginUserDetail() { }

    public LoginUserDetail(UserInfo user) {
        this();
        this.user = user;
        this.id = user.getId();
        this.username = user.getUsername();
    }

    @JsonIgnore
    public UserInfo getUser() {
        return user;
    }

    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @JsonIgnore
    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return username;
    }

    /** 账户是否未过期, 过期无法验证 */
    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /** 指定用户是否解锁, 锁定的用户无法进行身份验证 */
    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /** 指示是否已过期的用户的凭据(密码), 过期的凭据防止认证 */
    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /** 是否可用, 禁用的用户不能身份验证 */
    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }
}
